<?php 
defined('IN_DESTOON') or exit('Access Denied');
login();
require DT_ROOT.'/module/'.$module.'/common.inc.php';
require DT_ROOT.'/include/post.func.php';
require MD_ROOT.'/childcompany.class.php';

if(in_array($action, array('add', 'edit'))) {
	$COM_TYPE = explode('|', $MOD['com_type']);
	$COM_SIZE = explode('|', $MOD['com_size']);
	$COM_MODE = explode('|', $MOD['com_mode']);
	$MONEY_UNIT = explode('|', $MOD['money_unit']);
	$BANKS = explode('|', trim($MOD['cash_banks']));
}
$do = new childcompany();
switch($action) {
	case 'add':
		if($submit) {
                       $param   = filter_input_post($_POST['member']);	
                        if(empty($param['email'])){
                             message('请输入邮箱地址');
                        }
                        if ($db->get_one("SELECT username FROM {$DT_PRE}member WHERE username='{$param['username']}'")) {
                            message('会员登录名已经存在');
                        }
                        
                        if ($db->get_one("SELECT company FROM {$DT_PRE}member WHERE company='{$param['company']}'")) {
                            message('公司名称已经存在');
                        }
                     	$param['linkurl'] = userurl($param['username']);
                        $param['passport'] = $param['username'];
                        $password = $member['password'];
                        $param['passsalt'] = random(8);
                        $param['paysalt'] = random(8);
                        $param['password'] = dpassword($password, $param['passsalt']);
                        $param['payword'] = dpassword($password, $param['paysalt']);
                        $param['sound'] = 1;
                    
                        
                        $param['parent_id'] = $_userid;
                        $inset_id = $do->add($param);
                        if ($inset_id > 0){
                            dmsg($L['op_add_success'], '?action=index');
                        } else {
                            message('添加失败');
                        }
		        
		
		} else {

		        $head_title = '添加子公司';
		}
		break;
	case 'edit':
                if ((int)$_REQUEST['itemid'] <= 0) {
                    message('参数错误');
                }
              
                $itemid = (int)$_REQUEST['itemid'];
		$do->itemid = $itemid;
		$r = $do->get_one();
		if(!$r || $r['parent_id'] != $_userid) {
                    message('数据不存在');
                }
		if($submit) {
			        $param   = filter_input_post($_POST['member']);	
				$do->edit($param); 
                                $name = $param['company'];
                               // var_dump("update {$DT_PRE}company set company={$name} where userid=".$itemid);exit;
                                $db->query("update {$DT_PRE}company set company='{$name}' where userid=".$itemid);
                                dmsg($L['op_edit_success'], '?action=index');
				//message($do->errmsg);
		
		} else {
			extract($r);
			$head_title = '编辑子公司';
		}
	break;
	case 'delete':
		$itemid or message($L['friend_msg_choose']);
		$do->itemid = $itemid;
		$r = $do->get_one();
		if(!$r || $r['userid'] != $_userid) message();
		$do->delete($itemid);
		dmsg($L['op_del_success'], $forward);
	break;

    case 'order_list':
        $nav = isset($nav) ? intval($nav) : -1;
        $condition = "is_parent='$_userid'";
        if($nav == -1){$condition = $condition." and status in (3,7) ";}
        if($nav == 0){$condition = $condition." and  status =3 ";}
        if($nav == 1){$condition = $condition." and status=7 ";}

        $lists = $do->get_orderlist($condition);
        $r = $db->get_one("SELECT COUNT(*) AS num FROM ahroot_buy_6 WHERE is_parent='$_userid' and  status =3 ");
        $limit_used = $r['num'];
        $r1 = $db->get_one("SELECT COUNT(*) AS num FROM ahroot_buy_6 WHERE is_parent='$_userid'  and  status =7 ");
        $limit_used1 = $r1['num'];
        $head_title = '子公司订单';
    break;

    case 'merge':
        
        $itemids = is_array($itemid) ? implode(',', $itemid) : $itemid;
        $price = $db->get_one("SELECT SUM(price) price FROM {$DT_PRE}buy_6  WHERE itemid IN ($itemids)");
        $r = $db->query("update {$DT_PRE}buy_6 set status=7 WHERE itemid IN ($itemids) ");
        $userinfo = $db->get_one("SELECT userid,username,bank,money,locking,money+locking as total FROM {$DT_PRE}member WHERE userid=$_userid");
        $amount = $price['price']; 
        $db->query("UPDATE {$DT_PRE}member SET money=money+$amount WHERE userid=$_userid");
        $username = $userinfo['username'];
        $bank = $userinfo['bank'];
       
        $balance = $userinfo['money']+$price['price'];
        $reason = '合并订单-'.$price['price'];
        $note = '合并订单-'.$userinfo['username'];
        $editor = $userinfo['username'];
       // var_dump("UPDATE {$DT_PRE}member SET money=money+$amount, WHERE userid=$_userid");exit;
       
        $db->query("INSERT INTO {$DT_PRE}finance_record (username,bank,amount,balance,addtime,reason,note,editor) VALUES ('$username','$bank','$amount','$balance','$DT_TIME','$reason','$note','$editor')");
        
        dmsg('合并订单成功', $forward);
    break;

	default :
		$from = isset($from) ? $from : '';
		$condition = "parent_id=$_userid";
		
		$dfields = array('company', 'truename', 'career', 'telephone', 'mobile', 'homepage', 'email', 'qq');
		isset($fields) && isset($dfields[$fields]) or $fields = 0;
		$fields_select = dselect($sfields, 'fields', '', $fields);
		if($keyword) $condition .= " AND $dfields[$fields] LIKE '%$keyword%'";
		$r = $db->get_one("SELECT COUNT(*) AS num FROM {$DT_PRE}member WHERE $condition");
		$pages = pages($r['num'], $page, $pagesize);		
		$lists = array();
		$result = $db->query("SELECT *  FROM {$DT_PRE}member WHERE $condition ORDER BY userid DESC LIMIT $offset,$pagesize");
		while($r = $db->fetch_array($result)) {
			$lists[] = $r;
		}
		$head_title = '我的子公司';
}
include template('childcompany', $module);
?>